import AppKit

class DockTileManager {
    static let shared = DockTileManager()
    private var dockTile: NSDockTile
    
    private init() {
        dockTile = NSApplication.shared.dockTile
    }
    
    func updateProgress(_ progress: Double, posture: PostureType) {
        let size = NSSize(width: 128, height: 128)
        let image = NSImage(size: size)
        
        image.lockFocus()
        
        // 绘制背景
        NSColor.clear.set()
        NSBezierPath(rect: NSRect(origin: .zero, size: size)).fill()
        
        // 绘制进度圆环
        let center = NSPoint(x: size.width/2, y: size.height/2)
        let radius = size.width/2 - 10
        let startAngle: CGFloat = 90
        let endAngle = startAngle - progress * 360
        
        let path = NSBezierPath()
        path.lineWidth = 8
        posture.nsColor.set()
        path.appendArc(withCenter: center,
                      radius: radius,
                      startAngle: startAngle,
                      endAngle: endAngle,
                      clockwise: true)
        path.stroke()
        
        // 绘制图标
        if let symbolImage = NSImage(systemSymbolName: posture.dockIcon, accessibilityDescription: nil) {
            // 创建着色的图标
            let coloredImage = NSImage(size: symbolImage.size)
            coloredImage.lockFocus()
            
            // 设置天蓝色背景
            let skyBlue = NSColor(calibratedRed: 135/255, green: 206/255, blue: 235/255, alpha: 1.0)
            skyBlue.setFill()
            NSRect(origin: .zero, size: symbolImage.size).fill()
            
            // 使用混合模式绘制图标
            symbolImage.isTemplate = true
            symbolImage.draw(in: NSRect(origin: .zero, size: symbolImage.size),
                            from: NSRect(origin: .zero, size: symbolImage.size),
                            operation: .destinationIn,
                            fraction: 1.0)
            
            coloredImage.unlockFocus()
            
            // 绘制着色后的图标
            let iconSize: CGFloat = 70
            let symbolRect = NSRect(x: (size.width - iconSize)/2,
                                  y: (size.height - iconSize)/2,
                                  width: iconSize,
                                  height: iconSize)
            
            coloredImage.draw(in: symbolRect,
                             from: NSRect(origin: .zero, size: symbolImage.size),
                             operation: .sourceOver,
                             fraction: 1.0)
        }
        
        image.unlockFocus()
        
        dockTile.contentView = NSImageView(image: image)
        dockTile.display()
    }
} 